//
//  MLTopView.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/18.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "MLTopView.h"

@interface MLTopView ()

@property (nonatomic, strong) UIView *lineView; //竖线

@end

@implementation MLTopView

- (instancetype)initWithFrame:(CGRect)frame{
   
   self = [super initWithFrame:frame];
    
    if (self) {
     
        [self setupUI];
    }
    return self;
}
#pragma mark  - 重写set方法
- (void)setWeatherDTO:(WeatherDTO *)weatherDTO{

    _weatherDTO = weatherDTO;
    
    [self.weatherIconV sd_setImageWithURL:[NSURL URLWithString:weatherDTO.dayPictureUrl] placeholderImage:[UIImage imageNamed:@"duoyun"] options:SDWebImageRetryFailed];
    

    self.weatherTitleLab.text = weatherDTO.weather;
    
    self.weatherTemperatureLab.text = [NSString stringWithFormat:@"温度：%@",weatherDTO.temperature];
   NSString *dateString = [weatherDTO.date  componentsSeparatedByString:@"("][0];
    self.weatherDateLab.text = [NSString stringWithFormat:@"日期：%@",dateString];
    
    
}

/*
 //temperature = 32 ~ 25℃;
 //dayPictureUrl = http://api.map.baidu.com/images/weather/day/yin.png;
 //date = 周日 06月19日 (实时：31℃);
 */

- (void)setupUI{

    [self addSubview:self.lineView];
    [self addSubview:self.weatherIconV];
    [self addSubview:self.weatherTitleLab];
    [self addSubview:self.weatherAddressBtn];
    [self addSubview:self.weatherTemperatureLab];
    [self addSubview:self.weatherDateLab];

}



/** 约束 */
-(void)layoutSubviews{
    [super layoutSubviews];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
        make.centerX.mas_equalTo(self.mas_centerX);
        
    }];
   
    [self.weatherIconV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 40));
    }];
    
    
    CGFloat titleWidth = 45;
    if (kDevice_Is_iPhone5) {
        
        titleWidth = 35;
    }
    
    [self.weatherTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.weatherIconV.mas_right).offset(10);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(titleWidth);
        
    }];
    
    [self.weatherAddressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.lineView.mas_left).offset(-10);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.mas_equalTo(self.weatherTitleLab.mas_right).offset(10);
    }];
    
    
    [self.weatherTemperatureLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self.mas_centerX);
        make.right.mas_equalTo(0);
        make.centerY.mas_equalTo(self.mas_centerY).offset(-15);
    }];
    
    [self.weatherDateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(self.mas_centerX);
        make.right.mas_equalTo(0);
        make.centerY.mas_equalTo(self.mas_centerY).offset(15);
    }];
    
}


#pragma mark  - Actions
- (void)clickWeaButton:(UIButton *)btn{

    if (self.clickWeatherAddress) {
        self.clickWeatherAddress();
    }
}

#pragma mark  - Lazy
-(UIImageView *)weatherIconV{

    if (_weatherIconV == nil) {
        _weatherIconV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"duoyun"]];
        _weatherIconV.layer.cornerRadius = 3;
        _weatherIconV.layer.masksToBounds = YES;
    }
    return _weatherIconV;
}

-(UILabel *)weatherTitleLab{
    
    if (_weatherTitleLab == nil) {
        _weatherTitleLab = [[UILabel alloc] init];
        _weatherTitleLab.text = @"阴雨转晴天";
        _weatherTitleLab.font = FONT(15);
        _weatherTitleLab.textColor = kThemeColor;
//        _weatherTitleLab.backgroundColor = [UIColor redColor];
        _weatherTitleLab.numberOfLines = 0;
    }
    return _weatherTitleLab;
}



-(UIButton *)weatherAddressBtn{

    if (_weatherAddressBtn == nil) {
        _weatherAddressBtn = [[UIButton alloc] init];
//        _weatherAddressBtn.backgroundColor = [UIColor yellowColor];

        
        //加下划线
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:@"上海"];
        NSDictionary *dic = @{
                              NSFontAttributeName:FONT(16),
                              NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],
                              NSForegroundColorAttributeName:kThemeColor
                              };
        [content addAttributes:dic range:NSMakeRange(0, [content length])];
        [_weatherAddressBtn setAttributedTitle:content forState:UIControlStateNormal];
        
        [_weatherAddressBtn addTarget:self action:@selector(clickWeaButton:) forControlEvents:UIControlEventTouchUpInside];
        
        _weatherAddressBtn.lineBreakMode = NSLineBreakByTruncatingTail;
        
        _weatherAddressBtn.titleLabel.textAlignment = NSTextAlignmentCenter;

    }
    return _weatherAddressBtn;
}

-(UIView *)lineView{
    
    if (_lineView == nil) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _lineView;
}


-(UILabel *)weatherTemperatureLab{
    
    if (_weatherTemperatureLab == nil) {
        _weatherTemperatureLab = [[UILabel alloc] init];
        _weatherTemperatureLab.text = @"温度：30~25°C";
        _weatherTemperatureLab.textAlignment = NSTextAlignmentCenter;
        _weatherTemperatureLab.textColor = kThemeColor;
        _weatherTemperatureLab.font = FONT(15);

    }
    return _weatherTemperatureLab;
}


-(UILabel *)weatherDateLab{

    if (_weatherDateLab == nil) {
        _weatherDateLab = [[UILabel alloc] init];
        _weatherDateLab.text = @"日期：06月18日";
        _weatherDateLab.textAlignment = NSTextAlignmentCenter;
        _weatherDateLab.textColor = kThemeColor;
        _weatherDateLab.font = FONT(15);

    }
    return _weatherDateLab;
}




@end
